<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-descriptions :column="2" border >
            <el-descriptions-item :rowspan="2">
                <template #label>
                    <div class="cell-item">合理化建议</div>
                </template>
                <div><el-input class="no-border-input" style="width: 400px;" v-model="ruleForm.content" type="textarea" :autosize="{ minRows: 12 }" /></div>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">建议人　　</div>
                </template>
                <div style="width: 100px;"><el-text>{{ ruleForm.id ? ruleForm.user.name : user.name }}</el-text></div>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">日期　　　</div>
                </template>
                <div style="width: 100px;"><el-text>{{ _date }}</el-text></div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">是否被采纳</div>
                </template>
                <div><el-input class="no-border-input" style="width: 100%;" v-model="ruleForm.adopt" type="textarea" :autosize="{ minRows: 4 }"  /></div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">奖励　　　</div>
                </template>
                <div><el-input class="no-border-input" style="width: 100%;" v-model="ruleForm.reward" type="textarea" :autosize="{ minRows: 4 }"  /></div>
            </el-descriptions-item>
        </el-descriptions>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../util/request';
let user:any = sessionStorage.getItem('user')
user = JSON.parse(user)
const _date = ref<any>('')
const loading = ref<boolean>(false)
interface RuleForm {
    id: number
    user_id:number
    content:string
    status:number
    reward:string
    adopt:string
    user:any
    
}

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    user_id:0,
    status:0,
    content:'/',
    reward:'/',
    adopt:'',
    user:{}
})

const onGetData = () => {
    const date = new Date();
    const isoString = date.toISOString(); // 输出：2023-10-05T14:30:00.000Z
    _date.value = isoString.split('T')[0]; // 截取日期部分
    if(!ruleForm.value.id){
        return
    }
    http.get('/suggest/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
        
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    content: [
        { required: true, message: '请输入合理化建议', trigger: 'blur' },
    ],
    
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/suggest',ruleForm.value)
            }else{
                request = http.put('/suggest',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
.cell-item{width: 80px;}
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}
/* 全局移除 el-input 边框 */
.no-border-input {
/* 覆盖 Element Plus 的 CSS 变量 */
	--el-input-border-color: transparent;
	--el-input-focus-border-color: transparent;
	--el-input-hover-border-color: transparent;
}

/* 配合移除阴影 */
:deep(.no-border-input .el-input__inner) {
	box-shadow: none;
}
</style>
